<?php

/**
 * Élément de base des objets
 *
 * @package cass
 * @author Mickaël CASTANHEIRO <mickael.castanheiro@gmail.com>
 * @version 1.0
 */

namespace cass;

abstract class Element {

    /**
     * Retourne le nom de la classe courante
     */
    static public function getClassName() {
        return get_called_class();
    }

    /**
     * Simule les accesseurs
     * @param <string> $_name Nom de l'accesseur
     * @param <array> $_arguments Arguments de l'accesseur
     */
    public function __call($_method, $_arguments) {
        $threePrefixes = array('get', 'set');
        $prefixWithThree = substr($_method, 0, 3);

        if (substr($_method, 0, 2) == 'is') {
            $attribute = lcfirst(substr($_method, 2));
            if ($this->{$attribute} === true)
                return true;
            if (!empty($this->{$attribute}))
                return true;
            return false;
        }

        elseif (in_array($prefixWithThree, $threePrefixes)) {
            $attribute = lcfirst(substr($_method, 3));
            switch ($prefixWithThree) {
                case 'get':
                    if (is_array($this->{$attribute}) AND (sizeof($_arguments) == 1)) {
                        return $this->{$attribute}[$_arguments[0]];
                    } else {
                        return $this->{$attribute};
                    }
                    break;

                case 'set':
                    if (is_array($this->{$attribute}) AND (sizeof($_arguments) == 2)) {
                        $this->{$attribute}[$_arguments[0]] = $_arguments[1];
                    } else {
                        $this->{$attribute} = $_arguments[0];
                    }
                    break;
            }
        } elseif (substr($_method, 0, 5) == 'addTo') {
            $attribute = lcfirst(substr($_method, 5));
            if (sizeof($_arguments) == 2) {
                $this->{$attribute}[$_arguments[0]] = $_arguments[1];
            } else {
                $this->{$attribute}[] = $_arguments[0];
            }
        }
    }

}

?>
